Skip to content

Audit predict, match and train, and stop trusting a word the reference carries - #40

Merged
Paururo merged 12 commits into
mainfrom
fix/paralogous-kmer-guard
Nov 1, 2026
Merged

Paururo merged 12 commits into
mainfrom
fix/paralogous-kmer-guard

Conversation

@Paururo

@Paururo Paururo commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

What this is

An audit of the three subcommands nobody had been through (predict, match, train), plus the two panel problems left open by #39. Every finding below was reproduced against the compiled binary before it was touched, and the numbers quoted are measured on this machine.

The marker panels (classify, split-fastq)

A k-mer says what it matched, never where. Of the WHO catalogue markers split-fastq indexes, 5 have an ALT k-mer that H37Rv already carries elsewhere, and 67 have a second copy of their reference window. classify indexes a different set (it keeps indels and skips alleles longer than 11 bases) and finds 2 and 71. Those words are in every genome, so the first five read as present everywhere, the ancestor included, whatever the sample carries.

The reference is now read once while the index is built, and a marker with a copy is settled by its anchors or reported as alt_kmer_in_reference / ref_kmer_repeated. Counting the copies is what keeps this quiet in the ordinary case: a genome that shows as many as the reference has has proved the marker absent. On H37Rv itself with the DR panel: 0 called, 0 undetermined.

Writing the test turned up a defect of its own. An anchor was trusted for occurring once in the genome, but when the neighbouring change had broken the real locus, the paralogous copy was the one left looking unique, and the allele was read from the wrong place. An anchor now has to be unique in the reference as well.

The app's resistance card showed a marker's catalogue label as if it were the sample's change. A catalogue row is one base of a codon, and its label is the amino acid that base alone would give; a sample with two changes in the same codon fires the rows of both bases, each with its own label, while its codon reads a third amino acid. On such a genome the card now shows the amino acid the sample's codon reads, with the catalogue label beside it as the entry that matched, and leaves the rows whose label agrees untouched.

predict

before after
a record of 10,000 Ns L3 0.5100 0.0200 L2:0.49 Undetermined, Kmers_Used 0
a record with one 21-mer the same row as a real genome the same call, Kmers_Used 1
a FASTA of records shorter than k Not Enough Data: No records found one Undetermined row each
1,000 records in, 940 out no count anywhere counted and warned

The filter measured byte length, not whether a k-mer could be cut. An N-masked consensus is long but holds no window of A, C, G and T, so every tree sent the missing features down the same branch and agreed, and that agreement is what Confidence measures. The new Kmers_Used column is what shows a call resting on almost nothing, which no confidence figure can.

Also: the cleanup deleted an .xlsx left by an earlier run even when --excel was not given (verified by md5, now preserved); 0048 became the number 48 in Excel while the TSV kept 0048, and 1e5 became 100000; and vote ties went to whichever lineage appeared later in the training FASTA.

match

The singleton filter switched itself on above 100,000 unique query k-mers, and the score was then taken over the remainder:

reads from 1 Mb at 0.15x unique k-mers filter reported score
1,450 97,064 not applied 0.8886
1,500 100,176 95.3% dropped 1.0000

Three per cent more data moved the score to a perfect 1.0000. Below a few fold coverage a real k-mer is seen once, so dropping singletons discards the evidence that tells two references apart and keeps the repeats, which match everything. The filter now applies only when asked, the default keeps every k-mer, and Query_Kmers reports the denominator. The same two inputs now give 0.8886 and 0.8897.

Three more: query k-mers were cut with the newlines of a wrapped FASTA left in, so the same megabase gave 999,970 k-mers on one line and 571,420 at 70 columns; Query_Files came out of a HashMap and printed six different orders over twenty identical runs; and the Excel line named the .tsv it was asked for rather than the .xlsx it wrote.

The app offered five controls that reached nothing (max_ref_freq, coarse_top_n, coarse_stride, auto_tune, the early-stop pair) and a checkbox whose tooltip promised "percentages remain comparable with older reports" while the flag was never read. They are gone, replaced by --min-kmer-count, which is the one setting that does change the answer and was the only one not exposed.

train

  • The stratified CV restarted at fold 0 for every class, so a fold only ever received a member of a class with more members than its index. With 20 lineages of three genomes and --cv-folds 10, seven folds came out empty, were skipped with a warning, and the summary still said "10-fold" over the three that ran, each holding out a third of the data. The cursor now carries across classes: 0 empty folds.
  • An accuracy nobody measured was printed as 0.00%. accuracy_pct is now absent rather than zero, and the CLI and the app say "not evaluated".
  • A panel small enough that --min-samples-leaf forbids every split trained a forest of stumps and saved it without a word. With 10 sequences: Every tree is a single leaf, so this model predicts one lineage for every input, naming the reason and the fix. With 24: 17 of the 100 trees are a single leaf.
  • The single split is a plain shuffle, so a lineage with few genomes can miss the test set entirely and the headline accuracy then describes only the others. It now says so: The test set covers 4 of 12 lineages.
  • Entries skipped for having no sequence are counted, so "Read 800 sequences" from a file of 1,000 can be reconciled.

The two decisions, measured before taking them

Neither touches the published 498/498: that figure compares predict with phylogenetic placement on 498 independent RefSeq genomes, and does not come from train's internal test split.

Both were measured on synthetic genomes built on the MTB ancestor with the lineage panel's own SNPs plus 150 private SNPs each, six per lineage to train and four held out to predict, including the hardest case the panel offers: the M. bovis sublineages A4.1 to A4.8, two to five SNPs of their own apart.

The feature buckets stay as they are. The bucket is the last ten bases of the canonical k-mer rather than a hash of the whole of it, so the feature space is the ten-mers whatever k is (H37Rv occupies 27.8% of the buckets at k=11 and 28.3% at k=31). Measured against a full 64-bit hash:

lineages own SNPs each these buckets full hash
L1 to L7 and A4 41 to 294 32/32 32/32
L4.1 to L4.9 6 to 8 36/36 36/36
A4.1 to A4.8 2 to 5 32/32 32/32

The full hash gave slightly more decisive votes at twice the training time and would invalidate every model already trained, so it is not adopted; it is kept on exp/full-kmer-hash. The documentation no longer claims the 2-bit code "acts as a hash function", and the importance reports are described as candidates: a bucket holds every k-mer sharing its last ten bases. The coordinates report printed the canonical k-mer beside a forward-strand position, so 49.5% of its rows matched the genome at their coordinate; it now prints the bases found there, and all 6,379 rows of a test run match.

The single split is stratified. Same data, same model, only the held-out set changes:

reported accuracy lineages covered
plain shuffle 100.00% 3 of 6
stratified 87.50%, and 83.33% per lineage, naming L1 0/1 correct 6 of 6
out-of-bag, independent of both 89.29% all

--min-samples-leaf defaults to 1, in the CLI, the app and Galaxy. A node of twice that many samples or fewer is never split, and the default of 5 kept the trees too shallow to separate lineages with few genomes (on a panel of ten genomes it leaves every tree a single leaf):

lineages leaf 5: calls, mean margin leaf 1: calls, mean margin
L1 to L7 and A4 32/32, 0.75 32/32, 0.96
L4.1 to L4.9 36/36, 0.31 36/36, 0.77
A4.1 to A4.8 31/32, 0.15 32/32, 0.50

1 is the usual value for a random forest, and it was better on every panel measured.

Found while checking the documentation against real runs

An output named after an input destroyed the input. Every command creates its outputs before it has finished reading its inputs:

Command What happened to the input
classify -l run.tsv -o run the sample list was deleted, with a misleading "no valid genome entries"
predict -i p.fasta -o p.fasta the FASTA was deleted
match -r refs.fasta -o refs.fasta the references were replaced by the report, exit 0
classify -m panel.tsv -o panel the marker panel was replaced by the results, exit 0
train -i data.fasta -o data.fasta the training data was replaced by the model, exit 0

Each command now compares its outputs with its inputs once paths are resolved and stops before writing anything, naming both files.

A resistance summary could not be read back. lineage:count separates its pairs with spaces, but a catalogue path runs into the WHO grade, which has spaces in it, so one marker came apart into four pieces (41 for a genome with three markers). With a resistance panel the column now counts markers per drug, as undetermined:count already did; lineage panels are unchanged.

split-fastq lost k-mers across the line breaks of a wrapped FASTA, the same defect fixed in match above: the same sequence was called on one line and missed when wrapped at 70 columns. FASTQ reads have no line breaks inside a sequence, so their results do not change.

Every output example in the documentation is now an excerpt of a real run, checked line for line against the files the run wrote. The hand-written ones were inconsistent: a predict row whose vote shares added up to 1.04, classify rows placing a marker outside the gene they named, and a match denominator about 25 times lower than a real run gives. The input reference also describes the resistance panel shape the tools recognise, and how each WHO grade is read.

The input and output pages carry an invented example dataset. One small seeded dataset (a 4 kb reference, a lineage and a WHO-shaped panel, three samples, reads and a training set) goes through every command, and each page shows what pathotypr wrote for it, with the real-run excerpts kept below. Building it showed classify -l writing its detailed table in a different sample order on every run, because the list was read into a hash map; it now follows the list, and a test fails on the old behaviour.

Verification

87 + 83 + 16 tests pass, the GUI and the bench target compile, mkdocs build --strict is clean. Each new guard was deliberately broken to confirm the tests catch it: 7 of 7 mutations in the paralogue guards, 5 of 5 in the window logic, plus 11 checks of the label comparison against the real formats. The panel work is verified on a synthetic M. caprae genome built from the panel's own markers.

@github-actions github-actions Bot added documentation Improvements or additions to documentation module:train Model training module:predict Prediction from a trained model module:classify Marker calling on assemblies module:split-fastq Alignment-free genotyping from reads module:match Closest reference matching module:gui The Tauri desktop app area:markers Marker panels, their format and their validation area:galaxy Galaxy tool wrappers and their test data labels Nov 1, 2026
A k-mer says what it matched, never where, and some markers of the WHO catalogue
have an alternative k-mer the reference already holds elsewhere, or a reference
window it holds twice, so they read the same in every genome whatever the sample
carries. The reference is now read once while the index is built, and those
markers are settled by an anchor unique in the reference as well as in the
genome, or reported as not determined. The app's resistance card also shows the
amino acid the sample's own codon reads when it differs from the catalogue label.
A record with no usable k-mer, such as an N-masked consensus, used to get an
ordinary lineage call and confidence, and is now reported as Undetermined beside
a Kmers_Used count. Train no longer prints an unmeasured accuracy as 0.00%, deals
its cross-validation folds so none comes out empty, warns when trees never split,
and breaks vote ties by label rather than by file order. The Excel writer keeps
sample names as text, and predict's cleanup no longer deletes an earlier run's
workbook.
The singleton filter switched itself on above 100,000 unique k-mers and the score
was then a fraction of what remained, so 3% more reads could turn 0.8886 into a
perfect 1.0000; it now applies only when asked for, and the report carries the
denominator. Query k-mers are cut after removing the line breaks of a wrapped
FASTA, Query_Files comes out in the same order on every run, and the Excel log
names the file it wrote. The app's five match controls that reached nothing are
replaced by the one that changes the answer.
The single test split was a plain shuffle, so a lineage with few genomes was
often left out and the accuracy described only the others. Every lineage with two
genomes or more is now tested, and the report adds the mean accuracy per lineage,
naming those below 100%.
A node of twice --min-samples-leaf samples or fewer is never split, and the
default of 5 kept the trees too shallow to separate lineages with few genomes; it
is now 1 in the CLI, the app and Galaxy, which did better on every panel
measured. The documentation now says that a feature bucket is the last ten bases
of the canonical k-mer, which a full hash did not improve on, and the coordinates
report prints the bases found at each position instead of the canonical k-mer.
Every page was checked against the code and every figure measured again with the
final binary. The fixes cover a missing flag, examples without the new columns,
two pages still describing dropped records, missing undetermined reasons, one
figure that had never been measured, figures from before the paralogue guard, and
counts given to the wrong module.
@Paururo
Paururo force-pushed the fix/paralogous-kmer-guard branch from 26f0e38 to 51a8fe7 Compare November 1, 2026 16:03
Every command creates its outputs before it has finished reading its inputs, so
an output named after an input destroyed it: a sample list, a FASTA to predict,
the references for match, the marker panel and the training data could all be
truncated or replaced, and three of those runs exited successfully. Each command
now compares its outputs with its inputs once paths are resolved and stops with
a message naming both before anything is written.
The lineage:count column counts every prefix of a marker's path, which tallies
each level of a lineage hierarchy, but a catalogue path is not a hierarchy and
its prefixes run into the WHO grade, which has spaces in it. The space-separated
pairs then came apart and the column could not be read back; with a resistance
panel it now counts the markers found per drug, as undetermined:count already did.
split-fastq read each record's sequence with its newlines still in it, so every
k-mer spanning a line break of a wrapped FASTA was skipped as holding an invalid
base, and a variant whose k-mer crossed one was never seen. Both scans now read
the sequence without them; FASTQ reads have none, so their results do not change.
Every output example is now an excerpt of a real run, and the hand-written ones
they replace were inconsistent: a predict row whose vote shares added up to more
than one, classify rows placing a marker outside the gene it named, and match
counts no real run would produce. The input reference now describes the WHO
resistance panel shape the tools recognise, and several pages that still
described dropped records or the old columns say what the tools do today.
The list was read into a hash map, so the detailed table listed the samples in
a different order on every run, and which of two masked FASTA files sharing a
stem got the _2 suffix changed with it. Samples now come out in list order.
The input and output pages now run one small invented dataset through every
command and show what pathotypr wrote, with the real-run excerpts kept below.
The pass also fixed examples that disagreed with the code: marker rows whose REF
is not the reference base, the GFF naming rule, the match note and the predict rows.
@Paururo
Paururo force-pushed the fix/paralogous-kmer-guard branch from 81a0916 to 0fff018 Compare November 1, 2026 17:56
@Paururo
Paururo merged commit 58bb52c into main Nov 1, 2026
4 checks passed
@Paururo
Paururo deleted the fix/paralogous-kmer-guard branch November 1, 2026 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:galaxy Galaxy tool wrappers and their test data area:markers Marker panels, their format and their validation documentation Improvements or additions to documentation module:classify Marker calling on assemblies module:gui The Tauri desktop app module:match Closest reference matching module:predict Prediction from a trained model module:split-fastq Alignment-free genotyping from reads module:train Model training

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant